import { getSemester, getIndictor, getPJIndictor, saveStatement, getTermPJAndSummary, getTermPJ, getDictCode,getIndictConfigRows } from '@/api/home.js';


import { ElMessage } from 'element-plus';

export const useHomeStore = defineStore({
  id: 'home',
  state: () => ({
    semesterInfo: [],
    indicatorInfo: [],
    pjIndicatorInfo: [],
    selfStatementInfo: [],
    itemXQ: [],
    itemXQCK: [],
    dictCodeInfo: [],
    indictConfigRowsInfo: [],
  }),
  getters: {
    getSemesterInfo: (state) => {
      return state.semesterInfo || [];
    },
    getIndicatorInfo: (state) => {
      return state.indicatorInfo || [];
    },
    getPjIndicatorInfo: (state) => {
      return state.pjIndicatorInfo || [];
    },
    getSelfStatementInfo: (state) => {
      return state.selfStatementInfo || [];
    },
    getItemXQ: (state) => {
      return state.itemXQ || [];
    },
    getItemXQCK: (state) => {
      return state.itemXQCK || [];
    },
    getDictCodeInfo: (state) => {
      return state.dictCodeInfo || [];
    },
    getIndictConfigRowsInfo: (state) => {
      return state.indictConfigRowsInfo || [];
    },

  },
  actions: {
    // 学期数据
    async getSemesterData() {
      try {
        const res = await getSemester("2");
        this.semesterInfo = res;
      } catch (err) {
        ElMessage.error('获取学期信息失败');
      }
    },
    // 体测指标数据
    async getIndicatorData() {
      try {
        let res = await getIndictor();
        console.log(res)
        this.indicatorInfo = res;
      } catch (err) {
        ElMessage.error('获取指标信息失败');
      }
    },
    //成长记录对应的指标数据
    async getPjIndicatorData() {
      try {
        let res = await getPJIndictor();
        this.pjIndicatorInfo = res;
      } catch (err) {
        ElMessage.error('获取指标信息失败');
      }
    },
    //根据指标id获取指标配置项
    async getPjIndicatorConfigData(id) {
      try {
        let res = await getIndictConfigRows(id);
        this.indictConfigRowsInfo = res;
      } catch (err) {
        ElMessage.error('获取指标信息失败');
      }
    },
    //保存自我陈述
    async saveSelfStatement(data) {
      try {
        let res = await saveStatement(data);
        console.log(res);
        if (res && res.code > 0) {
          ElMessage.success('保存成功');
        } else {
          ElMessage.error('保存失败');
        }
      } catch (err) {
        ElMessage.error('保存失败');
      }
    },
    // 学期评价和综合评价用到的数据
    async getTermPJAndSummaryData(semesterid,stage) {
      try {
        let res = await getTermPJAndSummary(semesterid,stage);
        this.itemXQ = res;
      } catch (err) {
        ElMessage.error('获取指标信息失败');
      }
    },

    //学期评价
    async getTermPJData(semesterid,stage,stuid) {
      try {
        let res = await getTermPJ(semesterid,stage,stuid);
        this.itemXQCK = res;
      } catch (err) {
        ElMessage.error('获取学期评价指标信息失败');
      }
    },
    //综合评价

    //微代码
    async getDictCodeData() {
      try {
        let res = await getDictCode();
        this.dictCodeInfo = res;
      } catch (err) {
        ElMessage.error('获取微代码信息失败');
      }
    },
  }
});
